package com.draven.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.common.Result;
import com.draven.entity.FinanceRecord;
import com.draven.service.FinanceRecordService;
import com.draven.vo.FinanceStatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 收支记录控制器
 */
@RestController
@RequestMapping("/finance")
public class FinanceRecordController {

    @Autowired
    private FinanceRecordService financeRecordService;

    /**
     * 添加收支记录
     */
    @PostMapping("/add")
    public Result<Long> add(
            HttpServletRequest request,
            @NotNull(message = "类型不能为空") @RequestParam Integer type,
            @NotNull(message = "金额不能为空") @DecimalMin(value = "0.01", message = "金额必须大于0") @RequestParam BigDecimal amount,
            @RequestParam(required = false) String category,
            @NotNull(message = "记录时间不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam Date recordTime,
            @RequestParam(required = false) String remark) {
        Long userId = (Long) request.getAttribute("userId");
        Long id = financeRecordService.add(userId, type, amount, category, recordTime, remark);
        return Result.success(id);
    }

    /**
     * 修改收支记录
     */
    @PostMapping("/update")
    public Result<Void> update(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id,
            @NotNull(message = "类型不能为空") @RequestParam Integer type,
            @NotNull(message = "金额不能为空") @DecimalMin(value = "0.01", message = "金额必须大于0") @RequestParam BigDecimal amount,
            @RequestParam(required = false) String category,
            @NotNull(message = "记录时间不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam Date recordTime,
            @RequestParam(required = false) String remark) {
        Long userId = (Long) request.getAttribute("userId");
        financeRecordService.update(id, userId, type, amount, category, recordTime, remark);
        return Result.success();
    }

    /**
     * 删除收支记录
     */
    @PostMapping("/delete")
    public Result<Void> delete(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        financeRecordService.delete(id, userId);
        return Result.success();
    }

    /**
     * 获取收支记录详情
     */
    @GetMapping("/detail")
    public Result<FinanceRecord> detail(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        FinanceRecord detail = financeRecordService.getDetail(id, userId);
        return Result.success(detail);
    }

    /**
     * 分页查询收支记录
     */
    @GetMapping("/page")
    public Result<Page<FinanceRecord>> page(
            HttpServletRequest request,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String category,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date endTime,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = (Long) request.getAttribute("userId");
        Page<FinanceRecord> page = financeRecordService.page(userId, type, category, startTime, endTime, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 统计收支情况
     */
    @GetMapping("/stat")
    public Result<FinanceStatVO> stat(
            HttpServletRequest request,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date endTime) {
        Long userId = (Long) request.getAttribute("userId");
        FinanceStatVO stat = financeRecordService.stat(userId, startTime, endTime);
        return Result.success(stat);
    }

    /**
     * 按分类统计收支情况
     */
    @GetMapping("/statByCategory")
    public Result<List<FinanceStatVO.CategoryStat>> statByCategory(
            HttpServletRequest request,
            @RequestParam(required = false) Integer type,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) Date endTime) {
        Long userId = (Long) request.getAttribute("userId");
        List<FinanceStatVO.CategoryStat> stats = financeRecordService.statByCategory(userId, type, startTime, endTime);
        return Result.success(stats);
    }
} 